#import "FlutterDeviceInfoPlugin.h"
#import "FMDeviceTool.h"
#import "FMDeviceDiskTool.h"
#import "FMNetReachableTool.h"
#import "FMFaceBookTool.h"
#import "FMDeniedAccessAlertTool.h"
#import "FMContactController.h"
#import "FMContactTool.h"
#import "FMLocationTool.h"
#import <StoreKit/StoreKit.h>

@implementation FlutterDeviceInfoPlugin

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  FlutterMethodChannel* channel = [FlutterMethodChannel
      methodChannelWithName:@"flutter_device_info_plugin"
            binaryMessenger:[registrar messenger]];
  FlutterDeviceInfoPlugin* instance = [[FlutterDeviceInfoPlugin alloc] init];
  [registrar addMethodCallDelegate:instance channel:channel];
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  if ([@"getIDFA" isEqualToString:call.method]) {      
    result([FMDeviceTool getFMIDFA]);
  }else if ([@"getIDFV" isEqualToString:call.method]) {
      result([FMDeviceTool getFMIDFV]);
  }else if ([@"getAppVersion" isEqualToString:call.method]) {
      result([FMDeviceTool getFMAppVersion]);
  }else if ([@"getSystemVersion" isEqualToString:call.method]) {
      result([FMDeviceTool getFMSystemVersion]);
  }else if ([@"getSystemName" isEqualToString:call.method]) {
      result([FMDeviceTool getFMSystemName]);
  }else if ([@"getOriginalDeviceInfo" isEqualToString:call.method]) {
      result([FMDeviceTool getOriginalDeviceInfo]);
  }else if ([@"getDeviceNameplate" isEqualToString:call.method]) {
      result([FMDeviceTool getDeviceNameplate]);
  }else if ([@"getLocalTimeZone" isEqualToString:call.method]) {
      result([FMDeviceTool getLocalTimeZone]);
  }else if ([@"getLocalLanguageCode" isEqualToString:call.method]) {
      result([FMDeviceTool getLocalLanguageCode]);
  }else if ([@"getWifiInfo" isEqualToString:call.method]) {
      result([FMDeviceTool getWifiInfo]);
  }else if ([@"isSimulator" isEqualToString:call.method]) {
      result(@([FMDeviceTool isFMSimulator]));
  }else if ([@"isPhoneJailbreak" isEqualToString:call.method]) {
      result(@([FMDeviceTool isPhoneJailbreak]));
  }else if ([@"getTotalDiskSize" isEqualToString:call.method]) {
      result([FMDeviceDiskTool getFMTotalDiskSize]);
  }else if ([@"getTotalMemorySize" isEqualToString:call.method]) {
      result([FMDeviceDiskTool getFMTotalMemorySize]);
  }else if ([@"getAvailableDiskSize" isEqualToString:call.method]) {
      result([FMDeviceDiskTool getFMAvailableDiskSize]);
  }else if ([@"getAvailableMemorySize" isEqualToString:call.method]) {
      result([FMDeviceDiskTool getFMAvailableMemorySize]);
  }else if ([@"getNetReachableType" isEqualToString:call.method]) {
      result([FMNetReachableTool getNetReachableType]);
  }else if ([@"reportFaceBook" isEqualToString:call.method]) {
      NSDictionary *dic = call.arguments;
      NSString *urlScheme = dic[@"urlScheme"];
      NSString *appId = dic[@"appId"];
      NSString *disPlayName = dic[@"disPlayName"];
      NSString *clientToken = dic[@"clientToken"];
      [FMFaceBookTool reportUrlScheme:urlScheme appId:appId disPlayName:disPlayName clientToken:clientToken];
  }else if ([@"showDeniedAccessAlert" isEqualToString:call.method]) {      
      [FMDeniedAccessAlertTool showMessage:call.arguments];
  }else if ([@"openContact" isEqualToString:call.method]) {
      FMContactTool *tool = [[FMContactTool alloc] init];
      tool.contactHandle = ^(NSDictionary * dic) {
          result(dic);
      };
      [tool openContact];
  }else if ([@"getContactInfo" isEqualToString:call.method]) {
      FMContactTool *tool = [[FMContactTool alloc] init];
      [tool getContactInfo:^(NSArray * _Nonnull array) {
          result(array);
      }];
  }else if ([@"startLocation" isEqualToString:call.method]) {
      FMLocationTool *tool = [FMLocationTool shareInstance];
      tool.contactHandle = ^(NSDictionary * dic) {
          result(dic);
      };
      [tool startLocation];
  }else if ([@"showSKStoreReview" isEqualToString:call.method]) {
      if (@available(iOS 14.0, *)) {
          UIScene *currentScene = [UIApplication sharedApplication].connectedScenes.allObjects.firstObject;
          if ([currentScene isKindOfClass:[UIWindowScene class]]) {
              [SKStoreReviewController requestReviewInScene:(UIWindowScene *)currentScene];
          }
      }
  }else {
    result(FlutterMethodNotImplemented);
  }
}


@end
